package cn.tony.game.super_tank;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

public abstract class Drawable {

    public int     x, y, w, h, ox, oy, dir = 0, oDir = 0b0010;

    public boolean isAlive = true, isPlayer = false, isNotCheckHit = false;;

    public int team ;
    
    public final void paint(Graphics g) {
        Color c = g.getColor();
        draw(g);
        g.setColor(c);
    };

    public abstract void draw(Graphics g);

    protected void move(int dir, int speed) {
        ox = x;
        oy = y;
        if ((dir & 0b1000) == 0b1000) {
            // <---
            x -= speed;
        } else if ((dir & 0b0010) == 0b0010) {
            // --->
            x += speed;
        }
        if ((dir & 0b0100) == 0b0100) {
            // ^
            y -= speed;
        } else if ((dir & 0b0001) == 0b0001) {
            // v
            y += speed;
        }
    }

    public void back() {
        if (!isPlayer) {
            if (x <= 0) {
                dir = dir & 0b0111;
                dir = dir | 0b0010;
            } else if (x >= Game.W - w) {
                dir = dir & 0b1101;
                dir = dir | 0b1000;
            }

            if (y <= 0) {
                dir = dir & 0b1011;
                dir = dir | 0b0001;
            } else if (y >= Game.H - h - 20) {
                dir = dir & 0b1110;
                dir = dir | 0b0100;
            }
        }
        x = ox;
        y = oy;
    }

    public boolean isOverstep() {

        if (x <= 0 || x >= Game.W - w) {
            return true;
        }
        if (y <= 0 || y >= Game.H - h - 20) {
            return true;
        }
        return false;
    }

    protected void draw(Graphics g, int tmpDir, ImageDirectional imageDir) {

        if ((tmpDir & 0b1100) == 0b1100) {
            // 左上
            drawOfDir(g, imageDir.getUpLeft());
        } else if ((tmpDir & 0b1001) == 0b1001) {
            // 左下
            drawOfDir(g, imageDir.getDownLeft());
        } else if ((tmpDir & 0b0110) == 0b0110) {
            // 右上
            drawOfDir(g, imageDir.getUpRight());
        } else if ((tmpDir & 0b0011) == 0b0011) {
            // 右下
            drawOfDir(g, imageDir.getDownRihgt());
        } else if ((tmpDir & 0b1000) == 0b1000) {
            // 左
            drawOfDir(g, imageDir.getLeft());
        } else if ((tmpDir & 0b0010) == 0b0010) {
            // 右
            drawOfDir(g, imageDir.getRight());
        } else if ((tmpDir & 0b0100) == 0b0100) {
            // 上
            drawOfDir(g, imageDir.getUp());
        } else if ((tmpDir & 0b0001) == 0b0001) {
            // 下
            drawOfDir(g, imageDir.getDown());
        }
    }

    private void drawOfDir(Graphics g, Image dirImage) {
        g.drawImage(dirImage, x, y, w, h, null);
    }

    protected int checkDir() {
        int tmpDir = dir;

        if (dir == 0) {
            tmpDir = oDir;
        } else {
            oDir = dir;
        }
        return tmpDir;
    }

    public void syncLocation(Drawable relative) {
        x = relative.x;
        y = relative.y - h;
    }

    public Rectangle createRectangle() {
        return new Rectangle(x, y, w, h);
    }

    public boolean isHit(Drawable d) {
        if (d.isNotCheckHit) {
            return false;
        }

        if (this.team == d.team) {
            return false;
        }
        if (!this.isAlive || !d.isAlive) {
            return false;
        }
        return this.createRectangle().intersects(d.createRectangle());
    }

}
